package com.example.onlineeducation.ui.mine

import android.content.Context
import android.content.Intent
import com.example.onlineeducation.App
import com.example.onlineeducation.R
import com.example.onlineeducation.base.BaseActivity
import com.example.onlineeducation.data.model.CertificateListDataModel
import com.example.onlineeducation.databinding.CertificateActivityBinding
import com.example.onlineeducation.databinding.CertificateItemBinding
import com.example.onlineeducation.viewmodel.CertificateVM
import com.yc.jetpacklib.extension.showToast
import com.yc.jetpacklib.extension.ycInitLinearLayoutManage
import com.yc.jetpacklib.extension.ycIsNotEmpty
import com.yc.jetpacklib.extension.ycLoadImageNet
import com.yc.jetpacklib.recycleView.YcPagingDataAdapter
import com.yc.jetpacklib.refresh.YcRefreshBaseUtil
import com.yc.jetpacklib.refresh.YcRefreshSpecialPagingUtil


class CertificateListActivity : BaseActivity<CertificateActivityBinding>(CertificateActivityBinding::inflate) {
    private val mViewModel: CertificateVM by ycViewModels()
    private lateinit var mRefreshUtil: YcRefreshBaseUtil<CertificateListDataModel>
    private var mProId: String? = null

    private val mCertificateDialog by lazy {
        CertificateDialogFragment()
    }

    companion object {
        fun newInstance(context: Context) {
            val intent = Intent(context, CertificateListActivity::class.java)
            context.startActivity(intent)
        }
    }

    private val mAdapter: YcPagingDataAdapter<CertificateListDataModel, CertificateItemBinding> by YcPagingDataAdapter.ycLazyInitApply(
        CertificateItemBinding::inflate, CertificateListDataModel.diffCallback
    ) {
        mOnUpdate = { item ->
            ivExam.ycLoadImageNet(item.examImageUrl,errorImg = R.drawable.ic_topic)
            tvScore.text = item.score
            tvProName.text = item.proName
            tvExamName.text = item.examName
            tvExamTime.text = item.examTime
        }
        mItemClick = {
            if (it.content.ycIsNotEmpty()) {
                if (!mCertificateDialog.isAdded) {//避免用户点太快闪退
                    mCertificateDialog.setImgUrl(it.content!!, it.sealImageUrl)
                    mCertificateDialog.show(supportFragmentManager, "CertificateDialogFragment")
                }
            } else {
                showToast("证书内容为空，请联系管理员")
            }
        }
    }

    override fun CertificateActivityBinding.initView() {
        actionbarLayout.setTitle(titleName = "我的证书")
        rvCertificateList.apply {
            adapter = mAdapter
            ycInitLinearLayoutManage()
        }
        mRefreshUtil = YcRefreshSpecialPagingUtil<CertificateListDataModel>(this@CertificateListActivity)
            .build(mAdapter, srlList, rvCertificateList, flRefresh) {
                mRefreshCall = {
                    getData()
                }
            }
        mRefreshUtil.startRefresh()

        App.mInstant.mViewModel.mProInfoEntity.observe {
            if (it != null) {
                mProId = it.proId
            } else {
                showToast("项目Id为空，请联系管理员")
            }
        }
        mViewModel.mCertificateListDataModel.observe {
            mRefreshUtil.acSetPagingData(it)
        }
    }

    private fun getData() {
        mViewModel.getCertificateListPager(mProId!!)
    }
}